/**
 * 初始化注释按钮
 */
window.addEventListener('load', () => {
    const loadSysLoginApi = rx.once(() => rx.loadScript('/static/js/module/system/api/sysLoginApi.js'))
    document.querySelector('#logout-btn').addEventListener('click', async () => {
            await loadSysLoginApi()
            if (await ajaxOption({
                res: sysLoginApi.logout(),
                danger: '注销失败',
                success: '注销成功，即将跳转到登录页',
            })) {
                location.href = '/system/login'
            }
        },
    )
})

const topNavigationBar = new Vue({
    el: '#top-navigation-bar',
    data() {
        return {
            currentUser: {},
            menuArray: [{
                label: '首页',
            }],
        }
    },
    methods: {
        editSysUser() {
            sessionStorage.setItem('SessionCacheKey', '14')
            forward('/system-config/modify/admin_privilege_admin_edit', this.currentUser)
        },
        goto({id, parentPath, link}) {
            if (id) {
                sessionStorage.setItem('SessionCacheKey', id)
                forward(link)
            }
        },
    },
    async mounted() {
        const menuList = (await sysMenuApi.loadSysMenuList()).filter(({menuType}) => menuType === SysMenu.MenuTypeMenu)
        const linkSysMenuMap = rx.arrayToMap(menuList, ({link}) => link)
        const idSysMenuMap = rx.arrayToMap(menuList, ({id}) => id)
        const {accessPath, params} = rx.parseUrl(location.href)
        if (accessPath === '/') {
            return
        }
        let id = sessionStorage.getItem('SessionCacheKey')
        if (idSysMenuMap.get(parseInt(id)) === undefined) {
            return
        }
        if (id) {
            const sysMenu = idSysMenuMap.get(parseInt(id))
            this.menuArray = [...sysMenu.parentPath.split(',').slice(1)].map(id => idSysMenuMap.get(parseInt(id)))
        } else {
            const sysMenu = linkSysMenuMap.get(params.pagePath)
            if (sysMenu) {
                this.menuArray = [...sysMenu.parentPath.split(',').slice(1)].map(id => idSysMenuMap.get(parseInt(id)))
            }
        }
        await rx.wait(() => window.rxajax)
        this.currentUser = await sysLoginApi.currentUser()
    },
})